#ifndef _COLLISION_SYSTEM_H_
#define _COLLISION_SYSTEM_H_

#include "CollisionComponent.h"
#include "Singleton.h"
#include "System.h"
#include <vector>


class CollisionSystem :  public fe::System, public Singleton<CollisionSystem>
{
public:
    CollisionSystem(){m_component.clear();}
    ~CollisionSystem(){m_component.clear();}

    inline void execute()
    {
        for(unsigned int i = 0; i < m_component.size(); i++)
        {
            if(!m_component[i])  // removes if not valid
            {
                m_component.erase(m_component.begin() + i);
                i--;
                continue;
            }
            for(unsigned int z = 0; z < m_component.size(); z++)
            {
                if(z != i)
                {
                    if(m_component[i]->rect.contains(m_component[z]->rect))
                    {
                        std::cout << "do shit " << std::endl;
                    }
                }
            }
            // this allows me to only check the players to the tiles and not the tiles to the players ( optimized )
            for(unsigned int y = 0; y < m_tiles.size(); y++)
            {
                if(!m_tiles[y]) {
                    m_tiles.erase(m_tiles.begin() + y);
                    y--;
                    continue;
                }
                if(m_component[i]->rect.contains(m_tiles[y]->rect))
                {
                    std::cout << "tile collision" << std::endl;
                }
            }
        }
    }

    void addComponent(Collision* cc) {
        if(cc->isTile){
            m_tiles.push_back(cc);
        }
        else{
            m_component.push_back(cc);
        }
    }
    Collision* getComponent(int index) { return m_component[index]; }
    Collision* getTile(int index) { return m_tiles[index]; }

private:
    std::vector<Collision*> m_component;
    std::vector<Collision*> m_tiles;  // only colliding tiles

};

#endif
